package com.office.reader;

import com.office.AbstractReader;
import com.pff.*;

import java.util.*;
public class EmailPstReader extends AbstractReader{
	
	    public static void main(String[] args)
	    {
	        new EmailPstReader("");
	    }

	    public EmailPstReader(String filename) {
	        try {
	            PSTFile pstFile = new PSTFile(filename);
	            System.out.println(pstFile.getMessageStore().getDisplayName());
	            processFolder(pstFile.getRootFolder());
	        } catch (Exception err) {
	            err.printStackTrace();
	        }
	    }

	    int depth = -1;
	    public void processFolder(PSTFolder folder)
	            throws PSTException, java.io.IOException
	    {
	        depth++;
	        // the root folder doesn't have a display name
	        if (depth > 0) {
	            printDepth();
	            System.out.println(folder.getDisplayName());
	        }

	        // go through the folders...
	        if (folder.hasSubfolders()) {
	            Vector<PSTFolder> childFolders = folder.getSubFolders();
	            for (PSTFolder childFolder : childFolders) {
	                processFolder(childFolder);
	            }
	        }

	        // and now the emails for this folder
	        if (folder.getContentCount() > 0) {
	            depth++;
	            PSTMessage email = (PSTMessage)folder.getNextChild();
	            while (email != null) {
	                printDepth();
	                System.out.println("Email: "+email.getSubject());
	                email = (PSTMessage)folder.getNextChild();
	            }
	            depth--;
	        }
	        depth--;
	    }

	    public void printDepth() {
	        for (int x = 0; x < depth-1; x++) {
	            System.out.print(" | ");
	        }
	        System.out.print(" |- ");
	    }
	
}
